package com.ushine.web.component.validation.rule;


import com.ushine.web.component.validation.annotation.SpecialChar;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <h3>验证是否存在特殊字符</h3>
 *
 * @Author: 邱鹏
 * @Date 2021-01-14 14:44
 * @CompileVersion 1.8
 * @Version 1.0
 */
public class SpecialCharValidator implements ConstraintValidator<SpecialChar, String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        String regEx = "[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(s);
        return !m.find();
    }
}
